package com.itheima.edu.info.manager.controller;

import com.itheima.edu.info.manager.domain.Student;
import com.itheima.edu.info.manager.service.StudentService;

import java.util.Scanner;

public abstract class BascStudentController {

    //将学生信息传递给service
     public StudentService  studentService = new StudentService();
     public Student[] stus = studentService.findAllStudent();
     Scanner sc = new Scanner(System.in);
    public final void start() {
       // Scanner sc = new Scanner(System.in);
        studentLoop:while (true) {
            System.out.println("-------------欢迎来到<学生>管理系统--------------");
            System.out.println("请输入您的选择: 1.添加学生  2.删除学生  3.修改学生  4.查看学生  5.退出");
            String choice = sc.nextLine();
            switch (choice) {
                case "1" :
                    //System.out.println("添加");
                    addStudent();
                    break;
                case "2":
                   // System.out.println("删除");
                    deleteStudentById();
                    break;
                case "3":
                   // System.out.println("修改");
                    updateStudent();
                    break;
                case "4":
                    //System.out.println("查看");
                    findAllStudent();
                    break;
                case "5":
                    System.out.println("感谢您使用学生管理系统，再见");
                    break studentLoop;
                default:
                    System.out.println("您的输入有误，请重新输入:");
                    break;

            }
        }
    }
//修改
    public final void updateStudent() {
        //Student[] stus = studentService.findAllStudent();
        if (stus==null){
            System.out.println("查无信息,请添加后重试.");
            return;
        }
        String updateId = inToInfo();
        Student stu = outInfo(updateId);
        studentService.updateStudent(updateId,stu);
        System.out.println("修改成功!");
    }
//删除
    public final void deleteStudentById() {
        //Student[] stus = studentService.findAllStudent();
        if (stus==null){
            System.out.println("查无信息,请添加后重试.");
            return;
        }
        String delId = inToInfo();
        studentService.deleteStudentById(delId);
        System.out.println("删除成功!");
    }
//查看
    public final void findAllStudent() {
        //Student[] stus = studentService.findAllStudent();
        if (stus==null){
            System.out.println("查无信息,请添加后重试.");
            return;
        }
        System.out.println("学号\t姓名\t年龄\t生日");
        for (int i = 0; i < stus.length; i++) {
            if (stus[i]!=null){
                System.out.println(stus[i].getId()+"\t"+stus[i].getName()+"\t"+stus[i].getAge()+"\t\t"+stus[i].getBirthday());
            }

        }

    }
//添加
    public final void addStudent() {
        //创建输入对象
       // Scanner sc = new Scanner(System.in);
        String id;
        while (true) {
            System.out.println("请输入学生学号:");
            id = sc.nextLine();
           boolean flag = studentService.isExists(id);
           if (flag){
               System.out.println("学号已被占用,请重新输入:");
           }else {
               break;
           }

        }
        Student stu = outInfo(id);
        //传递对象给dao添加
        boolean result = studentService.addStudent(stu);
        //判断学号是否存在
        if (result){
            System.out.println("添加成功");
        }else{
            System.out.println("添加失败");
        }
    }
    public final String inToInfo(){
        String Id;
        while (true){
            System.out.println("请输入您的学生学号:");
            Id = sc.nextLine();
            //判断ID是否存在
            boolean exists = studentService.isExists(Id);
            if (!exists){
                System.out.println("您输入的学号不存在,请重新输入:");
            }else{
                break;
            }
        }
        return Id;
    }

    public abstract Student outInfo(String id);

}
